package com.shunhao.mvvm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.shunhao.base.BaseActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope

/**
 *类作用描述:   MVVM架构  TODO 待优化 目前没空
 *@author      KangGuiYang
 *创建时间      2020/6/18 16:24
 *邮箱        kangguiyang2@gmail.com
 *公司        南京舜贝佳信息科技 Shunbega.com
 */
abstract class BaseViewModelActivity<VM : ViewModel> : BaseActivity(),
    CoroutineScope by MainScope() {

    private var mActivityProvider: ViewModelProvider? = null

    protected lateinit var mViewModel: VM

    /**
     * 创建viewModel
     *
     * @param modelClass viewModel自定义的类
     * @param <T>        数据
     * @return 返回该类需要的viewModel实例
    </T> */
    protected open fun <T : ViewModel?> getActivityViewModel(modelClass: Class<T>): T {
        if (mActivityProvider == null) {
            mActivityProvider = ViewModelProvider(this)
        }
        //mActivityProvider!![modelClass]
        return mActivityProvider!!.get(modelClass)
    }


}